Home | | Latest | About | Random
# `const` instances and member functions marked `const`
**(READ: Sections 15.4, 15.5)**
Let us now discuss how various `const` correctness issue arises when dealing with `struct`.
## Compiler generated default constructor and `const` instances.
If a `struct` does not have a user-defined constructor, then the compiler generates a **default constructor**, which is a constructor that takes no explicit parameters.
However if a `struct` with a compiler generated default constructor has a member variable that is a fundamental type, then we cannot create a `const` instance of this `struct`.
**Example.** Here line 6 gives a build error:
```cpp
struct C {
int x;
};
int main () {
C a;
const C b;
return 0;
}
```
So if we want a `const` instance of a struct with fundamental type member variables, then we need to provide a user-defined constructor.
**Example.** Now this works.
```cpp
struct C {
int x;
C () {}
};
int main () {
C a;
const C b;
return 0;
}
```
## Fact 1. A `const` instance of a `struct` has all member variables `const`.
Now say we have a `const` instance of a `struct`, then all its member variables become `const`, and cannot be modified.
**Example.** Here line 9 is a **build error**, as the instance `b` is `const`, its member variable `b.x` is also `const`, but we tried to modify `b.x` on line 9.
```cpp
struct C {
int x;
C () {}
};
int main () {
C a;
a.x = 10;
const C b;
b.x = 10;
return 0;
}
```
## Fact 2. A member function marked `const` makes the implicit parameter a reference to `const`.
Recall when calling a member function of a `struct`, an implicit parameter of reference type (that we call `self`, for our PIC10A class) is created in our member function scope, and we assign to it the name of the object calling it.
Say `C::f` is a member function of `struct C`. Then...
- If `C::f` is not marked `const`, then `self` is a regular reference (think of it of type `C&`)
- If `C::f` is marked `const`, then `self` is a reference to `const` (think of it of type `const C&`)
So, **a member function should be marked** `const` **when there is no way it can be responsible for** **mutating the instance that is referenced by the implicit parameter.**
**Example.** Here line 8 gives a build error. Draw a diagram to see why!
```cpp
struct C {
void f () {}
};
int main () {
C a;
const C b;
a.f();
b.f();
return 0;
}
```
Here the member function `C::f` is not marked `const`, so when we call `b.f()`, we create an implicit parameter `self` that is a non-`const` reference. However, we then try to assign to `self` the implicit argument `b`, which is `const`. This violate `const`-correctness, we cannot use a non-`const` reference to reference a `const`!
**Example. ** Now,`C::f` is marked `const`. So calling `C::f` by any instance would create the implicit parameter `self` as a reference to `const`, which can be used to reference both `const` and non-`const` implicit arguments.
```cpp
struct C {
void f () const {}
};
int main () {
C a;
const C b;
a.f();
b.f();
return 0;
}
```
**Example. ** Here line 16 is a build error. Draw a diagram to see why!
```cpp
#include
using namespace std;
struct Point {
double x, y;
Point (double _x, double _y) : x(_x), y(_y) {}
void print() {
cout << '(' << x << ", " << y << ')' << endl;
}
};
int main () {
Point a(1, 2);
const Point b(3, 4);
a.print();
b.print();
return 0;
}
```
**Example.** Here line 5 is a build error. The member function `C::f` is marked `const`, so the implicit parameter `self` is a reference to `const`, but we attempt to modify the value of `x` through this reference to `const`. Draw a diagram!
```cpp
struct C {
int x;
C () {}
void f() const {
x++;
}
};
int main () {
return 0;
}
```
**Example.** The following has a build error on line 4. Draw a diagram!
```cpp
struct C {
void f () {}
void g () const {
f();
}
};
int main () {
return 0;
}
```
If one is to call `C::g`, then an implicit parameter of type `const C&` is bound to the implicit argument (which is the caller object itself). Then if one executes the body of `C::g`, which calls `C::f`, another implicit parameter of type `C&` is to be bound to `const C&`, which is not allowed!
## Some consequences of Fact 1 and Fact 2.
To recap, we have
- Fact 1. If we have a `const` instance of a `struct`, then all member variables are `const`
- Fact 2. If a member function of a `struct` is marked `const`, then when called, the implicit parameter is a reference to `const`. If not marked `const`, then the implicit parameter is just a reference.
From these, we can synthesize the following consequences, which can be derived from both facts above:
- It is a build error for a `const` instance of a `struct` to call a member function that is not marked `const`.
- If a member function is marked `const`, then within the function body it is a build error to assign to a member variable of the implicit parameter of fundamental type (and often true of class type too).
- If a member function is marked `const`, then within the function body it is a build error to call a member function which is not marked `const` through the implicit parameter.
- If a member function of a `struct` is not marked `const`, then we cannot call it from a `const` instance of the `struct`.
Now go back to the examples above, and see which examples can be explained by one of these consequences!
## Practice.
Identify all the lines with errors. Comment all the lines with errors out. Then what is the output of the code?
```cpp
#include
using namespace std;
struct C {
int var;
C() : var(0) {}
void f() { ++var; cout << "no const: " << var << endl; }
void g() const { cout << " const: " << var << endl; }
void h() const;
};
int main() {
C a;
a.var = 1;
a.f();
a.g();
const C b;
b.var = 2;
b.f();
b.g();
return 0;
}
void C::h() const {
var = 4;
f();
g();
}
```